package com.morpheus.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		// ---get the SMS message passed in---
		Bundle bundle = intent.getExtras();
		SmsMessage[] msgs = null;
		String str = "";
		if (bundle != null) {
			// ---retrieve the SMS message received---
			// ---If SMS is from comfort, display.
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];
			for (int i = 0; i < msgs.length; i++) {
				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				String address = msgs[i].getOriginatingAddress();
				String body = msgs[i].getMessageBody().toString();
				String cab = "+6598890278";
				String cab2 = "71222";
				String cancel = "CANCEL";
				if (address.equals(cab) && !body.equals(cancel)) {
					str += "SMS from Pin Qi";
					str += " :";
					str += body;
					str += "\n";
					// ---display the new SMS message---
					Toast.makeText(context, str, Toast.LENGTH_LONG).show();
					// ---start third activity---
					Intent nextactivity = new Intent(context, Third.class);
					nextactivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(nextactivity);

				} else if (address.equals(cab2) && !body.equals(cancel)) {
					str += "SMS from Comfort & CityCab";
					str += " :";
					str += body;
					str += "\n";
					// ---display the new SMS message---
					Toast.makeText(context, str, Toast.LENGTH_LONG).show();
					// ---start third activity---
					Intent nextactivity = new Intent(context, Third.class);
					nextactivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(nextactivity);
				}

			}
		}

	}

}
